#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @File   : tsSocketServerServ.py
# @Email  : xuexia_xu@feidee.com
# @Time   : 2024/4/7 13:56
# @Desc   : 《Python核心编程第3版》2.6 Twisted 时间戳 TCP 服务器

# 还有问题，暂时无法跑通

from twisted.internet import protocol, reactor
from time import ctime

PORT = 21567


class TSServProtocol(protocol.Protocol):
    def connectionMade(self):
        clnt = self.clnt = self.transport.getPeer().host
        print("...connected from:", clnt)

    def dataReceived(self, data: bytes):
        self.wfile.write(b'[%s] %s' % (bytes(ctime(), 'utf-8'), data))


factory = protocol.Factory()
factory.protocol = TSServProtocol
print("waiting for connection...")
reactor.listenTCP(PORT, factory)
reactor.run()
